import random
import uuid
from Autotests.Suites.StressTest.Operations.CommonOperations import CommonOperations
from Config.RunConfig import RunConfig, Options


class UserOperations:
    def __init__(self):
        self._operationsList = [
            self._addUser,
            self._enumerateUsers,
            self._enumerateUserRoles,
            self._getUserInfo,
        ]
        if RunConfig().get(Options.StressTest.stressAllPartners) == 'True':
            self._operationsList.append(self._authenticate)
            self._operationsList.append(self._removeUser)
            self._operationsList.append(self._modifyUser)

        self._baseUser = {
            'partnerId': 0,
            'name': uuid.uuid4(),
            'password': "NAME12345@#",
            'roleId': random.randint(1, 9),
            'doubleCheckedEnabled': 'true',
            'doubleCheckedUserName': 'false',
            'emailAddress': "iaso1@tut.by"
        }

    def _getRandomOperation(self):
        return random.choice(self._operationsList)

    def _authenticate(self, manserv, partnerId):
        manserv.Authenticate(partnerName = 'IASO', userName = 'root', password = 'IASO1234',
            name = uuid.uuid4(), version = uuid.uuid4(), host = uuid.uuid4())

    def _addUser(self, manserv, partnerId):
        self._baseUser['partnerId'] = partnerId
        manserv.AddUser(**self._baseUser)

    def _addStressTestUser(self, manserv, partnerId):
        try:
            self._baseUser['partnerId'] = str(partnerId)
            self._baseUser['name'] = 'stresstestuser'
            self._baseUser['password'] = 'stresstestuser'
            self._baseUser['roleId'] = 1
            self._baseUser['doubleCheckedEnabled'] = 'false'
            manserv.AddUser(**self._baseUser)
        except Exception, e:
            print 'add stress test user fail' + str(e)
            pass

    def _enumerateUsers(self, manserv, partnerId):
        manserv.EnumerateUsers(partnerId = partnerId)

    def _enumerateUserRoles(self, manserv, partnerId):
        manserv.EnumerateUserRoles()

    def _getUserInfo(self, manserv, partnerId):
        result = manserv.EnumerateUsers(partnerId = partnerId)
        if len(result.userInfo) > 1:
            resultLenth = random.randint(1, len(result.userInfo)-1)
            name = result.userInfo[resultLenth].name
            manserv.GetUserInfo(partnerId = partnerId, name = name, password = "NAME12345@#")

    def _removeUser(self, manserv, partnerId):
        result = manserv.EnumerateUsers(partnerId = partnerId)
        if len(result.userInfo) % CommonOperations()._baseGrowthRate == 0:
            resultLenth = random.randint(1, len(result.userInfo)-1)
            id = result.userInfo[resultLenth].id
            manserv.RemoveUser(id = id)

    def _modifyUser(self, manserv, partnerId):
        result = manserv.EnumerateUsers(partnerId = partnerId)
        if len(result.userInfo) > 1:
            resultLenth = random.randint(1, len(result.userInfo)-1)
            id = result.userInfo[resultLenth].id
            self._baseUser['id'] = id
            self._baseUser['partnerId'] = partnerId
            self._baseUser['name'] = uuid.uuid4()
            self._baseUser['roleId'] = random.randint(1, 9)
            manserv.ModifyUser(**self._baseUser)
